package com.lhh.adminlte.controller;

import com.lhh.adminlte.entity.Qrcode;
import com.lhh.adminlte.model.JsonResponse;
import com.lhh.adminlte.model.PageResponse;
import com.lhh.adminlte.service.IQRCodeService;
import com.lhh.adminlte.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/30 10:41
 * @Description:
 */
@Controller
@RequestMapping(value = "qrcode", produces = "application/json; charset=UTF-8")
public class QRCodeController extends BaseController {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String PATH = "/qrcode";

    @Autowired
    private IQRCodeService iqrCodeService;

    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, String>> options = new ArrayList<Map<String, String>>();
        Map<String, String> option1 = new HashMap<>();
        option1.put("name", "待使用");
        option1.put("value", "1");
        options.add(option1);
        Map<String, String> option2 = new HashMap<>();
        option2.put("name", "已使用");
        option2.put("value", "2");
        options.add(option2);
        Map<String, String> option3 = new HashMap<>();
        option3.put("name", "已禁用");
        option3.put("value", "3");
        options.add(option3);
        modelAndView.addObject("options", options);
        modelAndView.setViewName(PATH + "/index");
        return modelAndView;
    }

    @RequestMapping("list")
    @ResponseBody
    public String list(@RequestParam("value") String value, @RequestParam("status") int status, @RequestParam("phone") String phone, @RequestParam("batch") String batch,
                       @RequestParam(value = "useDateStart", required = false) String useDateStartStr, @RequestParam(value = "useDateEnd", required = false) String useDateEndStr,
                       @RequestParam(value = "createDateStart", required = false) String createDateStartStr, @RequestParam(value = "createDateEnd", required = false) String createDateEndStr,
                       @RequestParam("displayStart") Integer displayStart, @RequestParam("displayLength") Integer displayLength) throws ParseException {
        Date useDateStart = null;
        Date useDateEnd = null;
        Date createDateStart = null;
        Date createDateEnd = null;

        if (useDateStartStr != null) {
            useDateStart = sdf.parse(useDateStartStr);
        }

        if (useDateEndStr != null) {
            useDateEnd = sdf.parse(useDateEndStr);
        }

        if (createDateStartStr != null) {
            createDateStart = sdf.parse(createDateStartStr);
        }

        if (createDateEndStr != null) {
            createDateEnd = sdf.parse(createDateEndStr);
        }

        List<Qrcode> qrcodes = iqrCodeService.selectQrCodes(batch, value, phone, status, useDateStart, useDateEnd, createDateStart, createDateEnd, displayStart, displayLength);
        long count = iqrCodeService.countQrCode(batch, value, phone, status, useDateStart, useDateEnd, createDateStart, createDateEnd);
        PageResponse pageResponse = new PageResponse(count, qrcodes);
        return new JsonResponse(200, "success", pageResponse).toString();
    }


    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestParam("bonus") int bonus, @RequestParam("amount") int amount) {
        Date date = new Date();
        long batchId = System.currentTimeMillis();
        for (int i = 0; i < amount; i++) {
            String value = UUIDUtils.getRandomUUID32();
            iqrCodeService.addQrCode(String.valueOf(batchId), bonus, value, date);
        }
        return new JsonResponse(200, "success", null).toString();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam("id") int id, @RequestParam("status") int status) {
        boolean res = iqrCodeService.updateQrCode(id, status);
        if (res) {
            return new JsonResponse(200, "success", null).toString();
        } else {
            return new JsonResponse(202, "fail", null).toString();
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestParam(value = "id") int id) {
        boolean res = iqrCodeService.deleteQrCode(id);
        if (res) {
            return new JsonResponse(200, "success", null).toString();
        } else {
            return new JsonResponse(201, "fail", null).toString();
        }
    }

    @RequestMapping(value = "use", method = RequestMethod.GET)
    @ResponseBody
    public String use(@RequestParam("value") String value, @RequestParam("phone") String phone) {
        boolean res = iqrCodeService.useQrCode(value, phone, new Date());
        if (res) {
            return new JsonResponse(200, "success", null).toString();
        } else {
            return new JsonResponse(203, "fail", null).toString();
        }
    }


    @RequestMapping(value = "download", method = RequestMethod.GET)
    public String download() {
        return "";
    }

}
